<template>
  <div>
    <FormPanel ref="form" :list="formPanl" :cols="2" :labelWidth="300" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="margin-left:10%; color:blue">
      <span>1.维修企业名称：必须和《道路运输经营许可证》中的注册名称保持一致</span>
      <br />
      <span>
        2.维修企业注册区域编码：填写《道路运输经营许可证》编码前6位或从国家统计局网站找到对应区县6位编码（
        <a href="http://api.qichedangan.cn/index_areaid" target="_blank">http://api.qichedangan.cn/index_areaid</a>）
      </span>
      <br />
      <span>3.维修企业密码只能输入数字和英文字母</span>
      <br />
      <span style="color:red">4.当是否上报的值变更为是时，系统自动将结算的工单明细上传至交通部</span>
      <br />
    </div>
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <div style="text-align: center;">
      <multiuse-button :style="{ display: registerBtn }" type="primary" :auth-list="auths" auth-mark="/repair/archivesManage/registManage/Allowregistrationandsave" @click="save"
        >注 册</multiuse-button
      >
      <multiuse-button :style="{ display: saveBtn }" type="primary" :auth-list="auths" auth-mark="/repair/archivesManage/registManage/Allowregistrationandsave" @click="save">保存</multiuse-button>
      <!-- <el-button @click="close" type="primary">退 出</el-button> -->
      <el-button type="primary" @click="register1">注 册</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { authority } from '@/utils/authMixin';
import { register, queryInformation,selectRegister } from '@/api/repair/archivesManage/registManage/index';

export default {
  name: 'registmanage',
  mixins: [dictionary, authority],
  data() {
    this.saveData = null;
    return {
      formPanl: this.createForm(),
      registerBtn: '',
      saveBtn: 'none'
    };
  },
  mounted() {
    this.query();
  },
  methods: {
    createForm() {
      var validatorFu = function(rule, value, callback) {
        //  var reg = /^[0-9a-zA-Z ]+$/;
        var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$/;
        if (!reg.test(value)) {
          return callback(new Error('密码长度要大于6位，由数字和字母组成'));
        }
        callback();
      };
      return [
        {
          type: 'INPUT',
          label: '厂商用户名',
          fieldName: 'ascName',
          rules: [{ required: true, message: '请输入厂商用户名', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '密码',
          fieldName: 'ascPassword',
          rules: [{ required: true, validator: validatorFu, trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '维修企业名称',
          fieldName: 'repairAscName',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '维修企业密码',
          fieldName: 'RepairCoPassword',
          rules: [{ required: true, validator: validatorFu, trigger: 'blur' }]
        },

        {
          type: 'INPUT',
          label: '维修企业注册区域编码',
          fieldName: 'repairCoCode',
          rules: [{ required: true, message: '请输入维修企业注册区域编码', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否上报',
          fieldName: 'isUpload',
          initialValue: 10041002,
          itemList: this.createDictList('1004')
        },
        {
          type: 'INPUT',
          label: '维修企业唯一标识',
          fieldName: 'repairCoId',
          rules: [{ required: true, message: '请输入维修企业唯一标识', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '道路经营许可证号/机动车维修经营许可证号',
          fieldName: 'dealerLicense',
          rules: [{ required: true, message: '请输入信息', trigger: 'blur' }]
        }
      ];
    },

    formChangeHandle(val) {
      this.saveData = val;
    },
    // 查询注册信息
    async query() {
      let res = await queryInformation();
      console.log(res.data, '结果的值');
      if (res.data.length > 0) {
        // 存在注册的情况下,置灰所有的
        this.formPanl.forEach(x => {
          x.initialValue = res.data[0][x.fieldName];
          x.disabled = true;
        });
        this.formPanl.find(item => item.fieldName === 'isUpload').disabled = false;
        this.registerBtn = 'none';
        this.saveBtn = '';
      }
    },

    // 注册操作提交表单
    async save() {
      console.log(this.saveData, '提交表单操作');
      this.$refs.form.SUBMIT_FORM();
      // 提交操作当返回为200时候提示保存成功，刷新当前页面
      // 如果提交失败返回错误信息
      if (this.saveData != null) {
        console.log(this.saveData);
        // 判断当注册按钮不为隐藏状态时候则为注册
        if (this.registerBtn == '') {
          let res = await register(this.saveData);
          if (res.resultCode == 200) {
            this.formPanl.forEach(x => {
              x.disabled = true;
            });
            this.formPanl.find(item => item.fieldName === 'isUpload').disabled = false;
            // 重构formPanl
            this.formPanl = [...this.formPanl];
            this.registerBtn = 'none';
            this.saveBtn = '';
            this.$notify.success({ title: '提示信息', message: '注册成功!' });
          } else {
            return this.$notify.error({ title: '提示信息', message: res.errMsg || '注册失败!' });
          }
        } else {
          // 保存
          let res = await register(this.saveData);
          if (res.resultCode == 200) {
            this.$notify.success({ title: '提示信息', message: '保存成功!' });
          } else {
            return this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
          }
        }
      }
    },
    async register1(){
      let res = await selectRegister();
      if (res.resultCode == 200) {
            this.$notify.success({ title: '提示信息', message: '保存成功!' });
          } else {
            return this.$notify.error({ title: '提示信息', message:  '保存失败!' });
          }
    }
    // //退出按钮清空所有数据
    // close() {
    //   this.$refs.form.RESET_FORM();
    // }
  }
};
</script>
